local alignment = require 'alignment'
local constraints = require 'constraints'
local rect = require 'rect'
local vec2 = require 'vec2'
local single_child_view = require 'views.single_child_view'

---@class aligned: single_child_view
---@field alignment vec2
local aligned = single_child_view:new_view_type()

---@class align.renderer: single_child_view.renderer
aligned.renderer = single_child_view.renderer:new_renderer_type()

---@param options { alignment: vec2, child: view }
function aligned:__call(options)
    self:set_view_prototype(options)

    options.alignment = options.alignment or alignment.center
    assert(options.child)

    return options
end

---@param render_rect rect
---@param view aligned
function aligned.renderer:render(render_rect, view)
    local render_constraints = constraints.new(vec2(0), render_rect:size())
    local child_constrained_size = 
        self.child_renderer:layout(render_constraints, view.child)
    local child_size = child_constrained_size:to_vec2_or(render_rect:size())
    local child_rect = rect
        .from_bottom_left(vec2(0), child_size)
        :align_in(render_rect, view.alignment)

    self.child_renderer:render(child_rect, view.child)
end

return aligned
